#include "sort.h"

static void swap(int *, int, int);
static void sort1(int *, int, int);

void sort(int *arr, int len)
{
    sort1(arr,0, len - 1);
}

/*
大停，小停，大小交换，相遇时相遇点和基准点交换，以相遇点为界限展开递归
*/
static void sort1(int *arr, int start, int end)
{
    if (start >= end)
    {
        return;
    }
    int star = arr[start];
    int l = start, r = end;
    while (l < r)
    {
        while ((arr[r] > star) && (l < r))
        {
            r--;
        }
        while ((arr[l] <= star) && (l < r))
        {
            l++;
        }
        if (l < r) 
        {
            swap(arr, l, r);
        }
        else 
        {
            swap(arr, start, l);
        }
    }
    sort1(arr, start, l -1);
    sort1(arr, l + 1, end);
}

static void swap(int *arr, int num1, int num2)
{
    int temp = arr[num1];
    arr[num1] = arr[num2];
    arr[num2] = temp;
}